import {HttpClient,HttpHeaders} from '@angular/common/http';
import { Injectable } from '@angular/core';
import {BaseUrl} from './common/api';
import {DataService} from './dataservice'
import { print } from 'util';
import * as _ from 'underscore';


@Injectable()

export class ChartsService{
    constructor (
        private http:HttpClient,
        private dataService:DataService
    ){
        
    }
    private lineOption={};
    private barOption={};
    private lineUrl='http://localhost:8080/dashboard/getAggregateData.do';
    private dataId=47;
    private saveurl = BaseUrl.savewidgeturl;
    private updataurl=BaseUrl.updateurl;
    private getAggregateurl=BaseUrl.getAgregateurl;
    getLineChart(){
        this.http.post(this.lineUrl,this.dataId).subscribe((data)=>{
            this.lineOption={
                title:{
                    show:true,
                    text:'-value',
                    backgroundColor:'rgba(167,255,80,0.5)',
                    textStyle:{
                        fontSize:12,
                        color:'#333'
                    }
                },
                tooltip: {
                    trigger:'axis'
                },
                legend: {
                    data: ['工厂']
                },
                xAxis: {
                    type : 'category',
                    boundaryGap : false,
                    name:'厂区',
                    data: ["DYPT", "DYSP", "JQCP", "JQNP", "JQSP", "NSSP",'WHPT']
                  },
                  yAxis: {
                    type : 'value',
                    name:'value',
                    data:[20,30,40,50,60,70,80]
                  },
                  series: [{
                    name: '线图',
                    type: 'line',
                    data: [0, 40, 55, 32, 50, 66,80],
                    smooth:true
                }]
            };
            return this.lineOption
        })
    }

    savenewidget(data){
        return this.http.post(this.saveurl,data)
    }

    //编辑中图表的保存
    editwidget(data){
        return this.http.post(this.updataurl,data)
    }

    //获取图表数据
    getAggRegate(dataid,cfg){
        return this.http.post(this.getAggregateurl,dataid,cfg)
    }


    getChartServices (chartConfig) {
        var chart;
        // switch (chartConfig.chart_type) {
        //     case 'line':
        //         chart = chartLineService;
        //         break;
        //     case 'pie':
        //         chart = chartPieService;
        //         break;
        //     case 'kpi':
        //         chart = chartKpiService;
        //         break;
        //     case 'table':
        //         chart = chartTableService;
        //         break;
        //     case 'funnel':
        //         chart = chartFunnelService;
        //         break;
        //     case 'sankey':
        //         chart = chartSankeyService;
        //         break;
        //     case 'radar':
        //         chart = chartRadarService;
        //         break;
        //     case 'map':
        //         chart = chartMapService;
        //         break;
        //     case 'scatter':
        //         chart = chartScatterService;
        //         break;
        //     case 'gauge':
        //         chart = chartGaugeService;
        //         break;
        //     case 'wordCloud':
        //         chart = chartWordCloudService;
        //         break;
        //     case 'treeMap':
        //         chart = chartTreeMapService;
        //         break;
        //     case 'areaMap':
        //         chart = chartAreaMapService;
        //         break;
        //     case 'heatMapCalendar':
        //         chart = chartHeatMapCalendarService;
        //         break;
        //     case 'heatMapTable':
        //         chart = chartHeatMapTableService;
        //         break;
        //     case 'liquidFill':
        //         chart = chartLiquidFillService;
        //         break;
        //     case 'contrast':
        //         chart = chartContrastService;
        //         break;
        //     case 'chinaMap':
        //         chart = chartChinaMapService;
        //         break;
        //     case 'chinaMapBmap':
        //         chart = chartChinaMapBmapService;
        //         break;
        //     case 'relation':
        //         chart = chartRelationService;
        //         break;
        // }
        return chart;
    };

    renders(containerDom, widget, optionFilter, scope, reload, persist, relations){
        // var deferred = $q.defer();
        var chart = this.getChartServices(widget.config);
        
        this.dataService.getDataSeries(widget, function (data) {
            console.log(data)
            try {
                var option = chart.parseOption(data);
                if (optionFilter) {
                    optionFilter(option);
                }
                if (data.drill) {
                    data.drill.drillDown = function (id, value, render) {
                        this.dataService.getDrillPath(widget.datasetId, id).then(function (path) {
                            var i = 0;
                            _.each(path, function (e, _i) {
                                if (e.id == id) {
                                    i = _i;
                                }
                            });
                            var node = path[++i];
                            _.find(widget.config.keys, function (e, _i) {
                                if (e.id == id) {
                                    e.type = '=';
                                    e.values = [value];
                                    if (!_.find(widget.config.keys, function (e) {
                                            return e.id == node.id;
                                        })) {
                                        widget.config.keys.splice(_i + 1, 0, node);
                                    }
                                    return true;
                                }
                            });
                            _.find(widget.config.groups, function (e, _i) {
                                if (e.id == id) {
                                    e.type = '=';
                                    e.values = [value];
                                    if (!_.find(widget.config.groups, function (e) {
                                            return e.id == node.id;
                                        })) {
                                        widget.config.groups.splice(_i + 1, 0, node);
                                    }
                                    return true;
                                }
                            });
                            this.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, function (data) {
                                var option = chart.parseOption(data);
                                if (optionFilter) {
                                    optionFilter(option);
                                }
                                render(option, data.drill.config);
                            });
                        });
                    };
                    data.drill.drillUp = function (id, render) {
                        _.find(widget.config.keys, function (e, _i) {
                            if (e.id == id) {
                                widget.config.keys[_i - 1].values = [];
                                widget.config.keys.splice(_i, 1);
                                return true;
                            }
                        });
                        _.find(widget.config.groups, function (e, _i) {
                            if (e.id == id) {
                                widget.config.groups[_i - 1].values = [];
                                widget.config.groups.splice(_i, 1);
                                return true;
                            }
                        });
                        this.dataService.getDataSeries(widget.datasource, widget.query, widget.datasetId, widget.config, function (data) {
                            var option = chart.parseOption(data);
                            if (optionFilter) {
                                optionFilter(option);
                            }
                            render(option, data.drill.config);
                        });
                    };
                }
            } finally {
                if (widget.config.chart_type == 'chinaMapBmap') {
                    chart.render(containerDom, option, scope, persist, data.drill);
                } else {
                    return new Promise((resolve,reject)=>{
                        chart.render(containerDom, option, scope, persist, data.drill, relations, widget.config).subscribe(resolve,reject)
                    })
                    // deferred.resolve(chart.render(containerDom, option, scope, persist, data.drill, relations, widget.config));
                }
            }
        }, reload,null,null,null);
        // return deferred.promise;
    }

}


// {"columnList":[{"index":0,"aggType":null,"name":"alarm_question_type"},{"index":1,"aggType":null,"name":"site"},{"index":2,"aggType":"sum","name":"value"}],"data":[["安全事故","DYPT","10"],["安全事故","DYSP","61"],["安全事故","JQCP","97"],["安全事故","JQNP","60"],["安全事故","JQSP","21"],["安全事故","NSSP","96"],["安全事故","WHPT","34"]]}